package com.yyk.yiliao.ui.activity.dingdan;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.bean.CartOrderlist;

import java.util.List;

/**
 * Created by ${zk} on 2018/7/22 0022.
 * <p>
 * <p>
 * 多布局插入广告 暂时还没有实现
 */

public class RecyclerViewCommodityTab_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    List<CartOrderlist.DataBean.ListBean> mList;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;
    final int TYPE_4 = 3;
    OnItemClickListener listener;
    private RecyclerView recyclerView;


    //在RecyclerView提供数据的时候调用
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 定义一个点击事件接口回调
     */
    public interface OnItemClickListener {
        void onItemClick(RecyclerView parent, View view, int position);
    }


    public RecyclerViewCommodityTab_Adapter(Context context, List<CartOrderlist.DataBean.ListBean> list) {
        mContext = context;
        mList = list;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

//        if (listener != null) {
////            holder.itemView.setBackgroundResource(R.drawable.recycler_bg);//设置背景
//        }
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (listener != null && view != null && recyclerView != null) {
//                    int position = recyclerView.getChildAdapterPosition(view);
//                    listener.onItemClick(recyclerView, view, position);
//                }
//            }
//        });
//        MyViewHolder holder1 = null;
//        MyViewHolder2 holder2 = null;
//        MyViewHolder3 holder3 = null;
//        MyViewHolder4 holder4 = null;
//        CartOrderlist.DataBean.ListBean dataBean = mList.get(position);
//        if (holder instanceof MyViewHolder) {
//            holder1 = (MyViewHolder) holder;
//            holder1.tv_sname.setText(dataBean.getSname());
//            holder1.tv_num.setText(dataBean.getNum());
//            holder1.tv_money.setText("¥" + dataBean.getMoney());
//            Glide.with(mContext).load(ApiService.BASE_URL + dataBean.getImg()).into(holder1.iv_img);
//
//        } else if (holder instanceof MyViewHolder2) {
//            holder2 = (MyViewHolder2) holder;
//            holder2.tv.setText(dataBean.getTitle());
//            String created_at = TimeUtil.YMDHMS(String.valueOf(dataBean.getCreated_at()));
//            holder2.tv2.setText(created_at);
//            holder2.tv3.setText("阅读" + dataBean.getNum());
//        } else {
//            holder3 = (MyViewHolder3) holder;
//            holder3.tv.setText(dataBean.getTitle());
//            String created_at = TimeUtil.YMDHMS(String.valueOf(dataBean.getCreated_at()));
//            holder3.tv2.setText(created_at);
//            holder3.tv3.setText("阅读" + dataBean.getNum());
//
//            HomeGetads_Info.DataBean.ArticleimgBean articleimg = dataBean.getArticleimg();
//            if (!TextUtils.isEmpty(articleimg.get_$1())) {
//                Glide.with(mContext).load(ApiService.BASE_URL + dataBean.getArticleimg().get_$1()).into(holder3.iv);
//            }
//            if (!TextUtils.isEmpty(articleimg.get_$2())) {
//                Glide.with(mContext).load(ApiService.BASE_URL + dataBean.getArticleimg().get_$2()).into(holder3.iv2);
//            }
//            if (!TextUtils.isEmpty(articleimg.get_$3())) {
//                Glide.with(mContext).load(ApiService.BASE_URL + dataBean.getArticleimg().get_$3()).into(holder3.iv3);
//            } else {
//                holder3.iv3.setVisibility(View.INVISIBLE);
//            }
//
//        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_1) {
            View view = View.inflate(mContext, R.layout.adapter_item_c_tab_two_two, null);
            return new MyViewHolder(view);
        } else if (viewType == TYPE_2) {
            View view2 = View.inflate(mContext, R.layout.adapter_item_c_tab_two_two, null);

            return new MyViewHolder2(view2);
        } else if (viewType == 3) {
            View view3 = View.inflate(mContext, R.layout.adapter_item_c_tab_two_two, null);
            return new MyViewHolder3(view3);
        } else {
            View view4 = View.inflate(mContext, R.layout.adapter_item_c_tab_two_two, null);
            return new MyViewHolder4(view4);
        }

    }

    /**
     * 获取每个位置的View
     */
    @Override
    public int getItemViewType(int position) {
        int type = mList.get(position).getId();
        Logger.e("代付款 待收货  已完成  已取消————" + type);
        if (type == 1) {
            return TYPE_1;
        } else if (type == 2) {
            return TYPE_3;
        } else if (type == 3) {
            return TYPE_3;
        } else {
            return TYPE_4;
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    /**
     * 代付款
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_sname;
        private TextView tv_num;
        private TextView tv_money;
        private TextView tv_total_num;
        private TextView tv_total_money;

        private ImageView iv_img;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_sname = itemView.findViewById(R.id.tv_sname);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_total_num = itemView.findViewById(R.id.tv_total_num);
            tv_total_money = itemView.findViewById(R.id.tv_total_money);
            iv_img = itemView.findViewById(R.id.iv_img);
        }
    }

    /**
     * 待收货
     */
    class MyViewHolder2 extends RecyclerView.ViewHolder {
        private TextView tv;
        private TextView tv2;
        private TextView tv3;

        public MyViewHolder2(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
        }
    }

    /**
     * 已完成
     */
    class MyViewHolder3 extends RecyclerView.ViewHolder {
        private TextView tv;
        private TextView tv2;
        private TextView tv3;
        private ImageView iv;
        private ImageView iv2;
        private ImageView iv3;

        public MyViewHolder3(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            iv = itemView.findViewById(R.id.iv);
            iv2 = itemView.findViewById(R.id.iv2);
            iv3 = itemView.findViewById(R.id.iv3);
        }
    }

    /**
     * 已取消
     */
    class MyViewHolder4 extends RecyclerView.ViewHolder {
        private TextView tv_sname;
        private TextView tv_num;
        private TextView tv_money;
        private TextView tv_total_num;
        private TextView tv_total_money;

        private ImageView iv_img;

        public MyViewHolder4(View itemView) {
            super(itemView);
            tv_sname = itemView.findViewById(R.id.tv_sname);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_total_num = itemView.findViewById(R.id.tv_total_num);
            tv_total_money = itemView.findViewById(R.id.tv_total_money);
            iv_img = itemView.findViewById(R.id.iv_img);
        }
    }


}
